import React, { Component } from 'react'
import { ListBox } from '../../../components'
import { fetchData } from '../../../components/AjaxConsumer'
import UpdateWorkareaPage from './UpdateNew'
import EditPage from './EditPage'
import InfoPage from './Info'
import UpdatePage from './Update'
import { Modal } from 'antd';

export default class ListProCountingTask extends Component {
    constructor(props) {
        super(props);
        //行操作列表
        this.rowOperation = [
            {
                handleName: 'doubleClick',
                showName: '行双击事件'
            }
        ];
        //header 操作列表
        this.headerOperation = [

            {
                handleName: 'create',
                showName: '创建',
                iconClass: 'plus'
            }, {
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            }, {
                handleName: 'info',
                showName: '查看',
                iconClass: 'info'
            }
        ];
        //table列表展示列配置
        //storageKey 本地存储的key
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏（用于列配置显示隐藏）, foreverHidden: true 列永久隐藏
        this.tableColumn = {
            storageKey: 'rawcountingTask',
            data: [
                {
                    showName: 'countingTaskId',
                    columnName: 'countingTaskId',
                    columnHidden: true,
                    foreverHidden: true
                }, {
                    showName: '盘点单号',
                    columnName: 'countingCode',
                    columnWidth: 100
                },
                {
                    showName: '合同编号',
                    columnName: 'contractNo',
                    columnWidth: 100
                }, {
                    showName: '型号类型',
                    columnName: 'modelType',
                    columnWidth: 100
                },
                {
                    showName: '盘点类型',
                    columnName: 'countingType',
                    showOption: [//页面显示配置
                        {
                            optionKey: '0',
                            optionShow: '合同号盘点'
                        }, {
                            optionKey: '1',
                            optionShow: '型号类型盘点'
                        }
                    ]
                }, {
                    showName: '计划时间',
                    columnName: 'planDate',
                    columnWidth: 140
                }, {
                    showName: '仓库',
                    columnName: 'whCode',
                    columnWidth: 100
                }, {
                    showName: '区域',
                    columnName: 'wdCode',
                    columnWidth: 100
                }, {
                    showName: '任务状态',
                    columnName: 'stauts',
                    showOption: [//页面显示配置
                        {
                            optionKey: '0',
                            optionShow: '创建'
                        }, {
                            optionKey: '1',
                            optionShow: '盘点中'
                        }, {
                            optionKey: '2',
                            optionShow: '盘点完成'
                        }
                    ],
                    columnWidth: 100
                }
            ]
        };
        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        //      5、下拉框 type='disabled'
        this.searchComponentData = [
            {
                showName: '型号类型',
                name: 'modelType',
                type: 'select',
                optionList: [
                   
                ]
            },
            {
                showName: '盘点类型',
                name: 'countingType',
                type: 'select',
                optionList: [//下拉框的信息
                    //下拉框的信息
                    {
                        optionKey: '',
                        optionShow: ''
                    }, {
                        optionKey: '0',
                        optionShow: '合同号盘点'
                    }, {
                        optionKey: '1',
                        optionShow: '型号类型盘点'
                    }
                ]
            }, {
                showName: '任务状态',
                name: 'stauts',
                type: 'select',
                optionList: [//下拉框的信息
                    //下拉框的信息
                    {
                        optionKey: '',
                        optionShow: ''
                    }, {
                        optionKey: '0',
                        optionShow: '创建'
                    }, {
                        optionKey: '1',
                        optionShow: '盘点中'
                    }, {
                        optionKey: '2',
                        optionShow: '盘点完成'
                    }
                ]
            },
            {
                showName: '计划时间',
                name: 'planDate',
                type: 'date',
                maxName: 'planDateMax', //最大值
                minName: 'planDateMin'  //最小值
            }
        ];
    }
    rowHandles = (strHandles, rowObj) => {
        if (strHandles == 'doubleClick') {
            this.infoHandle(rowObj);
        } else if (strHandles == 'info') {
            this.infoHandle(rowObj);
        } else if (strHandles == 'create') {
            this.createHandle(rowObj);
        } else if (strHandles == 'delete') {
            this.deleteHandle(rowObj);
        }
    };

    //删除盘点任务
    deleteHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        } else {
            this.refs.updateWorkarea.deleteHandle(rowObj[0].data)
        }
    }

    // 创建盘点任务
    createHandle = () => {
        console.log("创建盘点任务");
        this.refs.updateWorkarea.addWorkareaHandle();
    }



    infoHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            let pstData = {};
            pstData.countingCode = rowObj[0].data.countingCode;
            // pstData.materialCode = rowObj[0].data.materialCode;
            // pstData.productType = rowObj[0].data.productType;
            const options = {
                url: '/product/proCountingRecords/r/detail',
                type: 'GET',
                condition: pstData,
            };
            fetchData(this, options, (resData) => {
                this.refs.infoPage.editHandle(resData, pstData);
            });
        }
    };

    componentWillMount() {
        const dicType = 'model_type_p';
        const options= {
            url: '/base/dic/get/' + dicType,
            type: 'get',
            condition:{}
        };
        //先写死，后面再改
        fetchData(this, options, this.dicInitCallBackHandle);
    }

    //下拉框 初始化
    dicInitCallBackHandle = (resData)=>{
        //alert('resData' + JSON.stringify(resData, null, 4));
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';
        tmp.optionShow = '';
        optionList.push(tmp);
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.dicCode;
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        });

        //alert('optionList' + JSON.stringify(optionList, null, 4));
        this.tableColumn.data[3].showOption = optionList;
        this.searchComponentData[0].optionList = optionList;
    }

    //刷新列表
    refreshHandle = () => {
        this.refs.listOdsprostorage.refreshHandle();
    };
    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;
        return (
            <div>
                <ListBox
                    ref='listOdsprostorage'
                    title="成品盘点任务"
                    isHideLeft={isHideLeft}
                    tableColumnData={this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url='/product/proCountingTask/list'
                    rowHandles={this.rowHandles}
                    rowOperation={this.rowOperation}
                    headerOperation={this.headerOperation}
                    isRowSingle={true}
                />
                <UpdatePage
                    ref="updatePage"
                    refreshHandle={this.refreshHandle}
                />


                <InfoPage
                    ref="infoPage"
                    refreshHandle={this.refreshHandle}
                />

                <UpdateWorkareaPage
                    ref="updateWorkarea"
                    refreshHandle={this.refreshHandle}
                />


            </div>
        )
    }
}